package com.sxbbc.seller.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.files.RestUploadFileInfo;
import com.sxbbc.common.basics.files.UploadUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.BLogin;
import com.sxbbc.common.core.entity.jz.ProcFirstClassifyShop;
import com.sxbbc.common.core.entity.jz.ProcThirdClassifyMain;
import com.sxbbc.common.core.entity.jz.ProcThirdClassifyShop;
import com.sxbbc.common.core.enums.ClassifyTypeEnums;
import com.sxbbc.common.core.service.ProcClassifyMainService;
import com.sxbbc.common.core.service.ProcClassifyShopService;
import com.sxbbc.common.core.utils.FileUtils;
import com.sxbbc.common.core.utils.MultipartFileToFile;
import com.sxbbc.seller.util.BusinessSession;
import com.sxbbc.seller.util.SystemLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.sxbbc.seller.common.BProductMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * @author leehao
 * @description 商品规格/分类控制层
 * @date 2019-04-30 13:43
 */
@Controller
public class ClassifyController {

    @Autowired
    private ProcClassifyShopService procClassifyShopService;
    @Autowired
    private ProcClassifyMainService classifyMainService;

    /* ------------------ 商品分类 ------------------ */

    /**
     * 分类列表
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = BProductMapping.SHOW_CLASSFIY_LIST)
    public ModelAndView showClassifyList(HttpServletRequest request) {
        Integer bid = BusinessSession.getBid(request);
        QueryWrapper<ProcFirstClassifyShop> ew = new QueryWrapper<>();
        ew.eq("status", StaticUtils.STATUS_YES).eq("type", ClassifyTypeEnums.BUSINESS.getId()).eq("target_id", bid);
        List<ProcFirstClassifyShop> list = procClassifyShopService.queryClassifyList(ew);
        ModelAndView view = new ModelAndView(BProductMapping.SHOW_CLASSFIY_LIST);
        view.addObject("list", list);
        return view;
    }

    /**
     * 新增编辑分类
     *
     * @param id       分类的id
     * @param editType 类型 1为新增；2为编辑
     * @param level    层级。一二三级
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = BProductMapping.SHOW_EDIT_CLASSFIY)
    public ModelAndView showClassifyEdit(Integer id, Integer editType, Integer level, ProcThirdClassifyMain third) {
        ModelAndView view = new ModelAndView(BProductMapping.SHOW_EDIT_CLASSFIY);
        view.addObject("level", level);
        view.addObject("editType", editType);
        view.addObject("third", third);
        view.addObject("id", id);
        return view;
    }

    /**
     * 带文件新增编辑分类
     *
     * @param imgUrl 分类图片
     * @param shop   分类对象
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = BProductMapping.EDIT_CLASSIFY)
    @ResponseBody
    @SystemLog(methods = "添加/编辑分类", module = "商品管理")
    public RestResponse editClassifyWithFile(@RequestParam(required = false) MultipartFile imgUrl,
                                             ProcThirdClassifyShop shop,
                                             HttpServletRequest request) {
        BLogin bLogin = BusinessSession.getBLogin(request);
        if (bLogin == null) {
            return GetRest.getFail("登录异常，请重试");
        }
        if (StringUtils.isBlank(shop.getName())) {
            return GetRest.getFail("请输入分类名称");
        }
        if (shop.getName().length() > 4) {
            return GetRest.getFail("分类名称长度请勿大于4个字符");
        }
        if (shop.getSort() == null) {
            return GetRest.getFail("请输入分类排序");
        }
        if (shop.getLevel() != 0 && shop.getEditType() == 1 && imgUrl == null) {
            return GetRest.getFail("请上传图片");
        }

        shop.setType(ClassifyTypeEnums.BUSINESS.getId());
        shop.setTargetId(bLogin.getBid());
        // 分类图片上传
        /*RestUploadFileInfo result = UploadUtils.getInstance().uploadFileQN(imgUrl);
        if (result.isStatus()) {
            shop.setUrl(result.getData());
        } else if (!result.isStatus() && imgUrl != null) {
            return GetRest.getFail("图片上传失败，请重试");
        }*/

        try {
            File fe= MultipartFileToFile.multipartFileToFile(imgUrl);
            String url = FileUtils.up(fe);
            shop.setUrl(url);
        }catch (Exception e){
            return GetRest.getFail("图片上传失败，请重试");
        }

        return procClassifyShopService.addClassify(shop);
    }

    /**
     * 删除前查询分类下是否有数据
     * 只对一级分类和二级分类查询
     *
     * @param id    分类的id
     * @param level 层级
     *              1 一级分类
     *              2 二级分类
     * @return boolean
     * @author leehao
     */
    @RequestMapping(value = BProductMapping.QUERY_CLASSFIY_BYPID)
    @ResponseBody
    public boolean queryClassifyByPid(Integer id, Integer level) {
        return procClassifyShopService.queryClassifyByPid(id, level);
    }

    /**
     * 删除分类
     * 假删除
     *
     * @param id    分类id
     * @param level 层级
     *              1 一级分类
     *              2 二级分类
     *              3 三级分类
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = BProductMapping.DELETE_CLASSIFY_CATEGORY)
    @ResponseBody
    @SystemLog(methods = "删除商品分类", module = "商品管理")
    public RestResponse deleteClassCategory(Integer id, Integer level) {
        return procClassifyShopService.deleteClassCategory(id, level);
    }

    /**
     * ajax查询平台分类
     *
     * @param type 商品分类类型
     * @param pid  上一级id
     * @param flag 一二三级层级
     * @return java.util.List<?>
     * @author leehao
     */
    @RequestMapping(value = BProductMapping.QUERY_CLASSIFY_LIST_AJAX_PLAT)
    @ResponseBody
    public List<?> queryClassifyListByAjaxWithPlat(HttpServletRequest request, Integer type, Integer pid, Integer flag) {
        List<?> list = classifyMainService.queryClassifyListAjax(pid, type, null, flag);
        return list;
    }

    /**
     * ajax查询商家分类
     *
     * @param type 商品分类类型
     * @param pid  上一级id
     * @param flag 一二三级层级
     * @return java.util.List<?>
     * @author leehao
     */
    @RequestMapping(value = BProductMapping.QUERY_CLASSIFY_LIST_AJAX_BUSINESS)
    @ResponseBody
    public List<?> queryClassifyByAjaxWithBusiness(HttpServletRequest request, Integer type, Integer pid, Integer flag) {
        Integer bid = BusinessSession.getBid(request);
        List<?> list = procClassifyShopService.queryClassifyListAjax(pid, ClassifyTypeEnums.BUSINESS.getId(), bid, flag);
        return list;
    }
}
